import React, { useEffect } from 'react';
import { useNavigate, useLocation, useParams, useSearchParams } from 'react-router-dom';
import NoLogin from '@pages/noLogin';
import request from "@services/request";

let timeSwitch = null

const withNavigate = (Component) => {
  return (props) => {
    const [searchParams] = useSearchParams();
    const footer = props.footer
    const navigate = useNavigate();
    const location = useLocation();
    const params = useParams();

    const formatAddress = (value) => {
      const frontSix = value.slice(0, 6);
      const backSix = value.slice(-4);
      const middle = '...';
      return frontSix + middle + backSix;
    }

    useEffect(() => {
      const getUserData = async () => {
        if (props.path === '/address/manage') return
        const res = await request.get("app_server/user_info");
        props.setState(res);
      };

      timeSwitch = setInterval(getUserData, 30000);

      return () => {
        clearInterval(timeSwitch)
      }
    }, [])


    const method = {
      navigate,
      match: location,
      params,
      searchParams,
      formatAddress
    }

    if (props.isLogin === false) return <NoLogin {...props} />

    return (
      <>
        <Component {...props} {...method} />
      </>
    );
  };
};

export default withNavigate;